<template>
  <div class="login-bg">
    <div class="login-container">
      <div class="login-left">
        <div class="login-left-content">
          <h2>Welcome to</h2>
          <h1>招标文件智析系统</h1>
          <p>你的强有力招标文件分析工具</p>
        </div>
      </div>
      <div class="login-right">
        <h2>忘记密码</h2>
        <form @submit.prevent="handleVerify">
          <div class="input-group">
            <input
              type="text"
              v-model="phone"
              placeholder="请输入手机号"
            />
            <div v-if="phone && phoneInvalid" class="input-tip">手机号不正确</div>
          </div>
          
          <div class="input-group">
            <div class="verify-box">
              <input type="text" v-model="code" placeholder="请输入验证码" />
              <button type="button" class="verify-btn" @click="getCode" :disabled="countdown > 0">
                <span v-if="countdown === 0">获取验证码</span>
                <span v-else>{{ countdown }}秒后重试</span>
              </button>
            </div>
            <div v-if="showCodeTip" class="input-tip">{{ codeTipMsg }}</div>
          </div>
          
          <button type="submit">下一步</button>
        </form>
        <div class="login-footer">
          <router-link to="/login">返回登录</router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "ForgotPasswordPage",
  data() {
    return {
      phone: '',
      code: '',
      sentCode: '',
      codeSent: false,
      countdown: 0,
      timer: null,
      showCodeTip: false,
      codeTipMsg: '',
    };
  },
  computed: {
    phoneInvalid() {
      const phoneReg = /^1[3-9]\d{9}$/;
      return this.phone && !phoneReg.test(this.phone);
    }
  },
  methods: {
    getCode() {
      if (!this.phone) {
        this.triggerCodeTip('请输入手机号');
        return;
      }
      const phoneReg = /^1[3-9]\d{9}$/;
      if (!phoneReg.test(this.phone)) {
        this.triggerCodeTip('手机号不正确');
        return;
      }
      // 检查手机号是否已注册
      const registeredPhones = JSON.parse(localStorage.getItem('registeredPhones') || '[]');
      if (!registeredPhones.includes(this.phone)) {
        this.triggerCodeTip('账号不存在');
        return;
      }
      // 生成验证码
      const code = Math.floor(100000 + Math.random() * 900000).toString();
      this.sentCode = code;
      this.code = code;
      this.triggerCodeTip('验证码已发送');
      this.codeSent = true;
      this.countdown = 60;
      if (this.timer) clearInterval(this.timer);
      this.timer = setInterval(() => {
        this.countdown--;
        if (this.countdown <= 0) {
          clearInterval(this.timer);
          this.timer = null;
          this.codeSent = false;
        }
      }, 1000);
    },
    triggerCodeTip(msg) {
      this.codeTipMsg = msg;
      this.showCodeTip = true;
      setTimeout(() => {
        this.showCodeTip = false;
      }, 2000);
    },
    handleVerify() {
      if (!this.phone) {
        this.triggerCodeTip('请输入手机号');
        return;
      }
      if (this.phoneInvalid) {
        this.triggerCodeTip('手机号不正确');
        return;
      }
      if (!this.code) {
        this.triggerCodeTip('请输入验证码');
        return;
      }
      if (this.code !== this.sentCode) {
        this.triggerCodeTip('验证码错误');
        return;
      }
      // 验证通过，允许进入修改密码页面
      sessionStorage.setItem('canChangePassword', '1');
      sessionStorage.setItem('changePhone', this.phone);
      this.$router.push('/change-password');
    }
  },
  beforeUnmount() {
    if (this.timer) clearInterval(this.timer);
  }
};
</script>

<style scoped>
.login-bg {
  min-height: 100vh;
  background: url('../assets/img/background.png') no-repeat center center;
  background-size: cover;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.login-bg::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(255, 255, 255, 0.3);
  backdrop-filter: blur(5px);
  z-index: 1;
}

.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  margin-top: -80px;
  margin-left: -80px;
  z-index: 2;
}

.login-left, .login-right {
  min-height: unset;
  height: auto;
  overflow: hidden;
}

.login-left {
  background: #5a6b7f;
  color: #fff;
  border-radius: 80px;
  padding: 60px 60px 60px 60px;
  width: 900px;
  height: 380px;
  margin-right: -120px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.10);
  display: flex;
  flex-direction: column;
  justify-content: center;
  z-index: 1;
  position: relative;
}

.login-left-content {
  position: relative;
  z-index: 2;
  text-align: left;
}

.login-left h2 {
  font-size: 3rem;
  margin-bottom: 22px;
  font-weight: 700;
}

.login-left h1 {
  font-size: 3.2rem;
  margin-bottom: 22px;
  font-weight: 700;
}

.login-left p {
  font-size: 1.2rem;
  opacity: 0.9;
}

.login-right {
  background: #d4c4a8;
  border-radius: 80px;
  padding: 60px 40px;
  width: 320px;
  height: 580px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.10);
  display: flex;
  flex-direction: column;
  align-items: center;
  z-index: 2;
  position: relative;
  margin-left: -380px;
}

.login-right h2 {
  text-align: center;
  margin-bottom: 120px;
  font-size: 2.3rem;
  font-weight: 800;
  color: #fff;
  letter-spacing: 2px;
}

form {
  display: flex;
  flex-direction: column;
  gap: 40px;
  margin-top: -80px;
  width: 100%;
}

.input-group {
  position: relative;
  margin-bottom: 10px;
}

input {
  padding: 16px 20px;
  border: 2px solid #e1e5e9;
  border-radius: 12px;
  font-size: 1rem;
  transition: all 0.3s ease;
  background: #f8f9fa;
  width: 100%;
  box-sizing: border-box;
}

input:focus {
  outline: none;
  border-color: #7b90a7;
  background: #fff;
  box-shadow: 0 0 0 3px rgba(123, 144, 167, 0.1);
}

.input-tip {
  position: absolute;
  bottom: -25px;
  left: 5px;
  font-size: 0.85rem;
  color: #e74c3c;
}

.verify-box {
  position: relative;
  display: flex;
  gap: 10px;
  width: 100%;
}

.verify-box input {
  flex: 1;
  min-width: 0;
}

.verify-box input[type="text"] {
  width: 60% !important;
  margin-left: 0;
  margin-right: 0;
  flex: none !important;
}

.verify-btn {
  padding: 16px 20px;
  background: #7b90a7;
  color: white;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  font-size: 0.9rem;
  white-space: nowrap;
  transition: all 0.3s ease;
}

.verify-btn:hover:not(:disabled) {
  background: #6a7f95;
}

.verify-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
}

button[type="submit"] {
  padding: 16px 20px;
  background: #7b90a7;
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-top: 10px;
  width: 100%;
}

button[type="submit"]:hover {
  background: #6a7f95;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(123, 144, 167, 0.3);
}

.login-footer {
  margin-top: 40px;
  text-align: center;
  font-size: 0.9rem;
}

.login-footer a {
  color: #7b90a7;
  text-decoration: none;
  font-weight: 500;
}

.login-footer a:hover {
  text-decoration: underline;
}

.success-tip {
  color: #2ecc40;
  background: #eafbe7;
  border-radius: 6px;
  padding: 12px;
  margin-bottom: 16px;
  font-size: 1.1rem;
  text-align: center;
}

.error-tip {
  color: #e74c3c;
  background: #fbeaea;
  border-radius: 6px;
  padding: 12px;
  margin-top: 10px;
  margin-bottom: 10px;
  font-size: 1rem;
  text-align: center;
}
</style> 